﻿Public Class clsXmlWriter
    Private Const FILENAME As String = "KFZData.xml"


    Public Shared Sub WriteToXml(ByVal sPath As String, ByVal pRefuelingList As List(Of clsRefueling), ByVal pMiscList As List(Of clsMisc))
        Dim pXmlSettings As New Xml.XmlWriterSettings

        pXmlSettings.Indent = True
        pXmlSettings.IndentChars = " "

        sPath = IO.Path.Combine(sPath, FILENAME)

        Dim pXmlWriter As Xml.XmlWriter = Xml.XmlWriter.Create(sPath, pXmlSettings)


        pXmlWriter.WriteStartElement("Data")

        pXmlWriter.WriteStartElement("Refuelings")
        For Each pRefueling In pRefuelingList
            pXmlWriter.WriteStartElement("Refueling")
            pXmlWriter.WriteAttributeString("Date", pRefueling.RefuelDate.ToString("dd.MM.yyyy"))
            pXmlWriter.WriteAttributeString("Price", pRefueling.Price)
            pXmlWriter.WriteAttributeString("Liter", pRefueling.Liter)
            pXmlWriter.WriteAttributeString("PricePerLiter", pRefueling.PricePerLiter)
            pXmlWriter.WriteAttributeString("GasStation", pRefueling.GasStation)
            pXmlWriter.WriteAttributeString("LastDistance", pRefueling.LastDistance)
            pXmlWriter.WriteEndElement()
        Next
        pXmlWriter.WriteEndElement()

        pXmlWriter.WriteStartElement("Miscs")
        For Each pMisc In pMiscList
            pXmlWriter.WriteStartElement("Miscs")
            pXmlWriter.WriteAttributeString("Date", pMisc.MiscDate.ToString("dd.MM.yyyy"))
            pXmlWriter.WriteAttributeString("Name", pMisc.Description)
            pXmlWriter.WriteAttributeString("Price", pMisc.Cost)
            pXmlWriter.WriteAttributeString("Note", pMisc.Note)
        Next
        pXmlWriter.WriteEndElement()

        pXmlWriter.WriteEndElement()
        pXmlWriter.Close()
    End Sub

    Public Shared Sub ReadFromXml(ByVal sPath As String, ByRef pRefuelingList As List(Of clsRefueling), ByRef pMiscList As List(Of clsMisc))
        If sPath Is Nothing Then Return

        sPath = IO.Path.Combine(sPath, FILENAME)

        If Not IO.File.Exists(sPath) Then Return

        Dim pXmlReader As Xml.XmlReader = Xml.XmlReader.Create(sPath)

        Dim pRefueling As clsRefueling = Nothing
        Dim pMisc As clsMisc = Nothing

        With pXmlReader
            While .Read
                Select Case .NodeType
                    Case Xml.XmlNodeType.Element
                        If pRefueling IsNot Nothing Then
                            pRefuelingList.Add(pRefueling)
                            pRefueling = Nothing
                        ElseIf pMisc IsNot Nothing Then
                            pMiscList.Add(pMisc)
                            pMisc = Nothing
                        End If


                        If .Name = "Refueling" Then
                            pRefueling = New clsRefueling

                            While .MoveToNextAttribute
                                Select Case .Name
                                    Case "Date"
                                        pRefueling.RefuelDate = Date.Parse(.Value)
                                    Case "Price"
                                        pRefueling.Price = Single.Parse(.Value)
                                    Case "Liter"
                                        pRefueling.Liter = Single.Parse(.Value)
                                    Case "PricePerLiter"
                                        pRefueling.PricePerLiter = Single.Parse(.Value)
                                    Case "GasStation"
                                        pRefueling.GasStation = .Value
                                    Case "LastDistance"
                                        pRefueling.LastDistance = Single.Parse(.Value)
                                End Select
                            End While

                        ElseIf .Name = "Misc" Then
                            pMisc = New clsMisc

                            While .MoveToNextAttribute
                                Select Case .Name
                                    Case "Date"
                                        pMisc.MiscDate = Date.Parse(.Value)
                                    Case "Name"
                                        pMisc.Description = .Value
                                    Case "Price"
                                        pMisc.Cost = Single.Parse(.Value)
                                    Case "Note"
                                        pMisc.Note = .Value
                                End Select
                            End While

                        End If
                End Select
            End While

            .Close()
        End With

        pXmlReader = Nothing

    End Sub
End Class
